﻿Imports System.Data.OleDb

Public Class AddProfile
    Inherits System.Web.UI.Page

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("godzillaFilms").ConnectionString)
            Dim sql As String = "SELECT * FROM Profile WHERE FanName=@f1"
            Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
            cmd.CommandType = CommandType.Text
            cmd.Parameters.AddWithValue("@f1", User.Identity.Name)
            conn.Open()
            Dim dr As OleDbDataReader = cmd.ExecuteReader()
            If dr.HasRows() Then
                dr.Read()
                If Not IsDBNull(dr("FirstName")) Then tb_firstName.Text = dr("FirstName")
                If Not IsDBNull(dr("LastName")) Then tb_lastName.Text = dr("LastName")
                If Not IsDBNull(dr("Description")) Then tb_description.Text = dr("Description")
                If Not IsDBNull(dr("FavouriteMovie")) Then tb_movie.Text = dr("FavouriteMovie")
                If Not IsDBNull(dr("FavouriteMonster")) Then tb_monster.Text = dr("FavouriteMonster")
                If Not IsDBNull(dr("City")) Then tb_city.Text = dr("City")
                If Not IsDBNull(dr("Country")) Then tb_country.Text = dr("Country")
            End If
        End If
    End Sub

    Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Dim newFileName As String = ""
        Dim sql As String
        If fup_picture.HasFile Then
            Dim myGUID = Guid.NewGuid()
            newFileName = myGUID.ToString() & ".jpg"
            Dim fileLocation = Request.MapPath("ProfilePictures") & "/" & newFileName
            fup_picture.SaveAs(fileLocation)
        End If
        Dim conn As New OleDbConnection(ConfigurationManager.ConnectionStrings("godzillaFilms").ConnectionString)
        If fup_picture.HasFile Then
            sql = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,FavouriteMovie=@f4,FavouriteMonster=@f5,Picture=@f6,City=@f8,Country=@f9 WHERE FanName=@f7"
        Else
            sql = "UPDATE Profile SET FirstName=@f1,LastName=@f2,Description=@f3,FavouriteMovie=@f4,FavouriteMonster=@f5,City=@f8,Country=@f9 WHERE FanName=@f7"
        End If
        Dim cmd As OleDbCommand = New OleDbCommand(sql, conn)
        cmd.CommandType = CommandType.Text
        conn.Open()
        cmd.Parameters.AddWithValue("@f1", tb_firstName.Text)
        cmd.Parameters.AddWithValue("@f2", tb_lastName.Text)
        cmd.Parameters.AddWithValue("@f3", tb_description.Text)
        cmd.Parameters.AddWithValue("@f4", tb_movie.Text)
        cmd.Parameters.AddWithValue("@f5", tb_monster.Text)
        If fup_picture.HasFile Then cmd.Parameters.AddWithValue("@f6", "ProfilePictures/" & newFileName)
        cmd.Parameters.AddWithValue("@f8", tb_city.Text)
        cmd.Parameters.AddWithValue("@f9", tb_country.Text)
        cmd.Parameters.AddWithValue("@f7", User.Identity.Name)
        cmd.ExecuteNonQuery()
        conn.Close()
        Response.Redirect("Profile.aspx?FanName=" & User.Identity.Name)
    End Sub
End Class